package com.corona.game;

import com.corona.Game;

import java.util.Iterator;
import java.util.Locale;
import java.util.Map;

/* compiled from: Battle.java */
/* loaded from: pokewilds.jar:com/pkmngen/game/CheckEvo.class */
class CheckEvo extends Action {
    Pokemon pokemon;

    public CheckEvo(Pokemon pokemon, Action nextAction) {
        super(new Object[0]);
        this.pokemon = pokemon;
        this.nextAction = nextAction;
    }

    @Override // com.corona.game.Action
    public void step(Game game) {
        if (this.pokemon.gainedLevel) {
            this.pokemon.gainedLevel = false;
            String evolveTo = null;
            int i = 1;
            while (true) {
                if (i > this.pokemon.level) {
                    break;
                } else if (Specie.gen2Evos.get(this.pokemon.specie.name.toLowerCase(Locale.ROOT)).containsKey(String.valueOf(i))) {
                    evolveTo = Specie.gen2Evos.get(this.pokemon.specie.name.toLowerCase(Locale.ROOT)).get(String.valueOf(i));
                    break;
                } else {
                    i++;
                }
            }
            if (evolveTo == null && this.pokemon.happiness >= 120) {
                Map<String, String> evos = Specie.gen2Evos.get(this.pokemon.specie.name);
                Iterator<String> it = evos.keySet().iterator();
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    String evo = it.next();
                    if (!evo.equals("tr mornday") || !game.map.timeOfDay.equals("day")) {
                        if (!evo.equals("tr nite") || !game.map.timeOfDay.equals("night")) {
                            if (evo.equals("tr anytime")) {
                                evolveTo = evos.get(evo);
                                break;
                            }
                        } else {
                            evolveTo = evos.get(evo);
                            break;
                        }
                    } else {
                        evolveTo = evos.get(evo);
                        break;
                    }
                }
            }
            if (evolveTo == null) {
                Map<String, String> evos2 = Specie.gen2Evos.get(this.pokemon.specie.name);
                Iterator<Pokemon> it2 = game.player.pokemon.iterator();
                while (true) {
                    if (!it2.hasNext()) {
                        break;
                    }
                    Pokemon currPokemon = it2.next();
                    if (currPokemon != this.pokemon && evos2.containsKey(currPokemon.specie.name)) {
                        evolveTo = evos2.get(currPokemon.specie.name);
                        break;
                    }
                }
            }
            if (evolveTo == null) {
                Map<String, String> evos3 = Specie.gen2Evos.get(this.pokemon.specie.name);
                String[] strArr = this.pokemon.attacks;
                int length = strArr.length;
                int i2 = 0;
                while (true) {
                    if (i2 >= length) {
                        break;
                    }
                    String attack = strArr[i2];
                    if (evos3.containsKey(attack)) {
                        evolveTo = evos3.get(attack);
                        break;
                    }
                    i2++;
                }
            }
            if (evolveTo != null) {
                this.nextAction = new DisplayText.Clear(game, new WaitFrames(game, 3, new WaitFrames(game, 61, new DisplayText(game, "What? " + this.pokemon.nickname.toUpperCase(Locale.ROOT) + " is evolving!", (String) null, true, false, (Action) new WaitFrames(game, 51, new EvolutionAnim(this.pokemon, evolveTo, new PlayMusic(this.pokemon, new SplitAction(new SetField(game.musicController, "startEvolveMusic", true, null), new EvolutionAnim.HandleInput(this.pokemon, evolveTo, this.nextAction)))))))));
            }
        }
        game.actionStack.remove(this);
        game.insertAction(this.nextAction);
    }
}
